package com.dbecom.platform.foundation.batch;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.util.StringUtils;

public class JobUtil {
    protected static Logger log = LoggerFactory.getLogger(JobUtil.class);

    public static Resource getResource(final String resourcePath) {

        if (resourcePath == null) {
            return null;
        }

        Resource resource = null;

        String profile = System.getProperty("spring.profiles.active");
        if (!StringUtils.hasText(profile) || ("dev").equals(profile)) {
            resource = new ClassPathResource(resourcePath);
            return resource;
        }

        try {
            resource = new UrlResource(resourcePath);
        } catch (Exception e) {
            log.error("resource error : {}", e.toString());
            return null;
        }

        return resource;
    }

    public static String getCurrentDate() {
        TimeZone zone = TimeZone.getTimeZone("Asia/Shanghai");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS a");
        format.setTimeZone(zone);

        Calendar calendar = Calendar.getInstance();
        return format.format(calendar.getTime());
    }
}
